<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class OrderRober_m extends MY_Model
{

    const TBL = 'order_rober';

    function __construct()
    {
        parent::__construct();
    }

    public function updateOrCreate($data)
    {
        $order_rob = $this->findBy(["user_id" => $data['user_id'], "order_id" => $data['order_id']]);
        if (!empty($order_rob)) {
            $result = $this->update(["id" => $order_rob['id']], ["create_at" => date("Y-m-d H:i:s", time())]);
        } else {
            $data['create_time'] = date("Y-m-d H:i:s", time());
            $result              = $this->insert($data, true);
        }
        return $result;
    }

    public function getRobUsers($order_id)
    {
        $order_robs = $this->select(["order_id" => $order_id]);
        if ($order_robs) {
            return $order_robs;
        } else {
            return false;
        }
    }

    public function getRobersBy($order_id)
    {
        $this->db->select('order_rober.*,users.*');
        $this->db->from('order_rober');
        $this->db->join('users', 'order_rober.user_id = users.user_id', 'left');
        $this->db->where('order_rober.order_id', $order_id);
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
    }

    public function getApnsRobers($order_id)
    {
        $this->db->select('order_rober.*,apns.device_token');
        $this->db->from('order_rober');
        $this->db->join('apns', 'order_rober.user_id = apns.user_id', 'left');
        $this->db->where('order_rober.order_id', $order_id);
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
    }

}
